//
//  MyPublishViewController.m
//  used
//
//  Created by lanou on 16/7/20.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "MyPublishViewController.h"
#import "KLPushListViewController.h"

@interface MyPublishViewController ()<UITableViewDataSource, UITableViewDelegate, KLMyPublishCellDelegate>


@property (nonatomic, strong) NSMutableArray *goodsModels;


@property (nonatomic, strong) UITableView *tableView;


@property (nonatomic, strong) UIImageView *bgImage;


@end

@implementation MyPublishViewController

- (UIImageView *)bgImage {
    if (!_bgImage) {
        _bgImage = [[UIImageView alloc] initWithFrame:self.view.frame];
        _bgImage.image = [UIImage imageNamed:@"publish"];
    }
    return _bgImage;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH - 64)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = (kScreenH - 64) * 0.3;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (NSMutableArray *)goodsModels {
    if (!_goodsModels) {
        _goodsModels = [NSMutableArray array];
    }
    return _goodsModels;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigation];
    
    [self.view addSubview:self.bgImage];
    [self loadGoodsData];
    
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"KLMyPublishCell" bundle:nil] forCellReuseIdentifier:@"myPublish"];

    
    
    
    
    // 查询某一行的数据
//    BmobQuery *query = [BmobQuery queryWithClassName:@"Publish"];
//    [query getObjectInBackgroundWithId:@"86e1fa04c7" block:^(BmobObject *object, NSError *error) {
//        
//        BmobObject *images = [object objectForKey:@"imageurl"];
//        BmobQuery *imageQ = [BmobQuery queryWithClassName:@"images"];
//        [imageQ getObjectInBackgroundWithId:images.objectId block:^(BmobObject *image, NSError *error) {
//           
//            for (int i = 0; i < 7; i++) {
//            BmobFile *imageFile = [image objectForKey:[NSString stringWithFormat:@"image%d", i]];
//                if (imageFile) {
//                    NSLog(@"%@", imageFile.url);
//                }
//            }
//        }];
//        
//    }];
    
   

}

- (void)loadGoodsData  {
    
    BmobQuery *query = [BmobQuery queryWithClassName:@"Publish"];
    [query whereKey:@"user" equalTo:[BmobUser getCurrentUser]];
    [query orderByDescending:@"updatedAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        for (BmobObject *obj in array) {
            KLGood *goodModel = [[KLGood alloc] init];
            
            goodModel.goodsname = [obj objectForKey:@"goodsname"];
            goodModel.saleprice = [obj objectForKey:@"salePrice"];
            goodModel.originprice = [obj objectForKey:@"oringePrice"];
            goodModel.goodstype = [obj objectForKey:@"sort"];
            goodModel.desc = [obj objectForKey:@"desc"];
            goodModel.province = [obj objectForKey:@"province"];
            goodModel.city = [obj objectForKey:@"city"];
            
            BmobObject *images = [obj objectForKey:@"imageurl"];
            BmobQuery *imageQ = [BmobQuery queryWithClassName:@"images"];
            [imageQ getObjectInBackgroundWithId:images.objectId block:^(BmobObject *image, NSError *error) {
                
                NSMutableArray *imageurls = [NSMutableArray array];
                for (int i = 0; i < 7; i++) {
                    BmobFile *imageFile = [image objectForKey:[NSString stringWithFormat:@"image%d", i]];
                    if (imageFile) {
                        [imageurls addObject:imageFile.url];
                        
                    }
                }
                goodModel.imageurls = imageurls;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    if (self.goodsModels.count) {
                        self.bgImage.hidden = YES;
                    }else {
                         self.bgImage.hidden = NO;
                    }
                    
                    [self.tableView reloadData];
                });
            }];
            [self.goodsModels addObject:goodModel];
        }
    }];
}

-(void)setNavigation
{

    self.navigationItem.title = @"我的发布";
    self.view.backgroundColor = KLRGBColor(240, 240, 240);

}


#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.goodsModels.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KLMyPublishCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myPublish" forIndexPath:indexPath];
    cell.model = self.goodsModels[indexPath.row];
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KLPushListViewController *listVc = [[KLPushListViewController alloc] init];
    listVc.model = self.goodsModels[indexPath.row];
    [self.navigationController pushViewController:listVc animated:YES];
}


- (void)clickCollectionViewWithModel:(KLGood *)model {
 
    KLPushListViewController *listVc = [[KLPushListViewController alloc] init];
    listVc.model = model;
    [self.navigationController pushViewController:listVc animated:YES];
    
}

@end
